package single;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.ScannedRobotEvent;
import robocode.SkippedTurnEvent;
import robocode.TeamRobot;
import robocode.WinEvent;
import robocode.util.Utils;

public class CP3O extends TeamRobot {
	public int posRoboAdvX = 0;
	public int posRoboAdvY = 0;
	public double velRoboAdv = 0;
	public double angRobAdv = 45;
	public double distRoboAdv = 0;
	boolean direita = false;
	int timeScan = 0;
	ScannedRobotEvent scanEvent;

	public void run() {

		setColors(Color.orange, Color.black, Color.green);
		setBulletColor(Color.red);

		while (true) {

			if (timeScan++ > 1) {
				setTurnRadarRightRadians(Double.POSITIVE_INFINITY);

			}

			if (getOthers() == 0 && timeScan > 5) {
				onScannedRobot(scanEvent);
			}

//			if (pertoParede()) {
//
//				setAhead(60);
//
//			} else {
//
//				ahead(400);
//				descrobreDirecao();
//			}

			execute();
		}

	}

	public void onScannedRobot(ScannedRobotEvent e) {

		setaDadosRoboADV(e);
		scanEvent = e;
		timeScan = 0;
		posicionaRadar(e);

		if (!isTeammate(e.getName())) {

			if (distRoboAdv < 200 && distRoboAdv > 20) {
				direita = true;
				setTurnRight(angRobAdv + getGunCharge());
				setAhead(distRoboAdv);
				mira(angRobAdv + getGunCharge());
				fogo(distRoboAdv);
				// fire(2);
			} else if (distRoboAdv <= 20) {

				direita = false;
				setTurnLeft(angRobAdv + getGunCharge());
				setAhead(distRoboAdv);
				mira(angRobAdv + getGunCharge());
				fogo(distRoboAdv);
				// fire(3);
			} else if (distRoboAdv >= 200 && distRoboAdv < 300) {

				direita = true;
				setTurnRight(angRobAdv + getGunCharge());
				setAhead(distRoboAdv);
				mira(angRobAdv + getGunCharge());
				fogo(distRoboAdv);
				// fire(1);
			}
		} else {
			direita = true;
			setTurnRight(angRobAdv + getGunCharge());
			setAhead(distRoboAdv);
		}

	}

	public void posicionaRadar(ScannedRobotEvent e) {
		setTurnRadarRightRadians(Utils.normalRelativeAngle(getHeadingRadians()
				+ e.getBearingRadians() - getRadarHeadingRadians()) * 2);
	}

	public void onHitRobot(HitRobotEvent INI) {
		
		angRobAdv = INI.getBearing();
		
		if (!isTeammate(INI.getName())) {
			
			mira(angRobAdv + getGunCharge());
			setBack(10);
			setAhead(distRoboAdv);
			setFireBullet(3);
		} else {
			
			setTurnRight(angRobAdv);
			setAhead(50);
		}
	}

	public void onHitWall(HitWallEvent e) {
		setBack(20);
		//setAhead(30);
		setTurnLeft(angRobAdv + getGunCharge());
	}

	public void onHitByBullet(HitByBulletEvent e) {
		angRobAdv = e.getBearing();
		setTurnRight(e.getBearing());
		setAhead(30);

	}

	public void onSkippedTurn(SkippedTurnEvent e) {

		setAhead(10);

		if (direita) {

			setTurnLeft(90 - angRobAdv);
			direita = false;

		} else {

			setTurnRight(90 - angRobAdv);
			direita = true;
		}

	}

	public void onWin(WinEvent e) {
		setColors(Color.black, Color.white, Color.red);
		dancinha();
	}

	public double anguloRelativo(double ANG) {
		if (ANG > -180 && ANG <= 180)
			return ANG;
		double REL = ANG;
		while (REL <= -180)
			REL += 360;
		while (ANG > 180)
			REL -= 360;
		return REL;
	}

	public void mira(double Adv) {
		double A = getHeading() + Adv - getGunHeading();
		if (!(A > -180 && A <= 180)) {
			while (A <= -180)
				A += 360;
			while (A > 180)
				A -= 360;
		}

		turnGunRight(A);

	}

	private void setaDadosRoboADV(ScannedRobotEvent e) {

		angRobAdv = e.getBearing();
		distRoboAdv = e.getDistance();
		velRoboAdv = e.getVelocity();
	}

	public void dancinha() {
		setAhead(5);
		setTurnRight(360D);
		setTurnGunLeft(360D);
		setTurnRadarRight(30D);
	}

	public boolean pertoParede() {

		return (getX() < 50 || getX() > getBattleFieldWidth() - 50
				|| getY() < 50 || getY() > getBattleFieldHeight() - 50);
	}

	public void fogo(double Distancia) {

		if (Distancia > 200 || getEnergy() < 15)
			fire(1);
		else if (Distancia > 30)
			setFireBullet(2.5);
		else
			setFireBullet(3);
	}

	private void descrobreDirecao() {

		if (direita) {

			direita = false;
			setTurnRight(angRobAdv);

		} else {

			direita = true;
			setTurnLeft(angRobAdv);
		}
	}

	public void tiroFatal(double EnergiaIni) {
		double Tiro = (EnergiaIni / 4) + .1;
		fire(Tiro);
	}

}
